package com.hsgene.consumables.service.impl;

import com.hsgene.common.UsedAmont;
import com.hsgene.common.UsedType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.consumables.condition.UnitCondition;
import com.hsgene.consumables.domain.ConsumablesUnitInfo;
import com.hsgene.consumables.exception.ConsumablesErrorCode;
import com.hsgene.consumables.persistence.ConsumablesUnitRepository;
import com.hsgene.consumables.service.ConsumablesUnitService;
import com.hsgene.consumables.service.api.GeneratorService;
import com.hsgene.generator.dto.GeneratorDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库存单位service
 *
 * @author wxf
 * @date 2018/8/15 17:27
 **/
@Service
public class ConsumablesUnitServiceImpl implements ConsumablesUnitService {

    @Resource
    private ConsumablesUnitRepository unitRepository;

    @Resource
    private GeneratorService generatorService;

    /**
     * 添加耗材单位
     *
     * @param unitInfo 耗材单位信息
     * @return
     */
    @Override
    public int addUnitInfo(ConsumablesUnitInfo unitInfo) {
        //id
        ApiResult<GeneratorDto> generatorResult = generatorService.generate("material_unit", null);
        if (!generatorResult.isSucc()) {
            return 0;
        }
        unitInfo.setId(generatorResult.getData().getSerialNumber());
        ConsumablesUnitInfo unitDetailsInfo = unitRepository.getUnitDetailsInfoByInfo(unitInfo);
        if(unitDetailsInfo!=null){
            return -1;
        }
        return unitRepository.insertUnit(unitInfo);
    }

    /**
     * 查询耗材单位信息
     *
     * @param unitCondition 进货单位
     * @return
     */
    @Override
    public Pagination<ConsumablesUnitInfo> queryUnitInfo(UnitCondition unitCondition) {
        int totalCount = unitRepository.getCount(unitCondition);
        Pagination<ConsumablesUnitInfo> pagination = Pagination.newInstance(unitCondition.getLimitHelper(), totalCount);
        if(totalCount>0){
            List<ConsumablesUnitInfo> infos = unitRepository.queryUnitInfo(unitCondition);
                int rowNumber = (unitCondition.getLimitHelper().getPage() - 1) * unitCondition.getLimitHelper().getSize();
                for(int i=0,n=infos.size();i<n;i++){
                    ConsumablesUnitInfo consumablesUnitInfo = infos.get(i);
                    consumablesUnitInfo.setNumber(rowNumber + i + 1);
                }
                pagination.setResult(infos);
        }else{
            pagination.setResult(new ArrayList<>());
        }
        return pagination;
    }

    /**
     * 更新耗材单位信息
     *
     * @param unitInfo 耗材单位信息
     * @return
     */
    @Override
    public boolean updateUnitInfo(ConsumablesUnitInfo unitInfo) {
        unitInfo.setUpdateDateTime(new Date());
        unitRepository.updateUnitInfo(unitInfo);
        return true;
    }

    /**
     * 删除耗材信息
     *
     * @param id 耗材单位id
     * @return
     */
    @Override
    public ApiResult<Boolean> deleteUnitInfo(String id) {
/*        ConsumablesUnitInfo unitDetailsInfo = unitRepository.getUnitDetailsInfo(id);
        if(unitDetailsInfo!=null&&unitDetailsInfo.getIsUsed()==UsedType.USED.getCode()){
            return ApiResult.fail(ConsumablesErrorCode.DELETE_IS_OCCUPY);
        }*/
        boolean b = unitRepository.deleteInfo(id);
        return ApiResult.succ();
    }

    /**
     * 耗材单位详情
     *
     * @param unitId
     * @return
     */
    @Override
    public ConsumablesUnitInfo getUnitDetailsInfo(String unitId) {
        return unitRepository.getUnitDetailsInfo(unitId);
    }

    /**
     * 批量删除耗材单位
     * @param infoList
     * @return
     */
    @Override
    public ApiResult<ConsumablesUnitInfo> deleteUnitInfoMore(List<ConsumablesUnitInfo> infoList) {
        if (infoList==null || infoList.isEmpty()) {
            return ApiResult.fail(ConsumablesErrorCode.PARAMETER_OBJECT_CANNOT_BE_EMPTY);
        }
      /*  List<ConsumablesUnitInfo> infos=new ArrayList<>();
        List<ConsumablesUnitInfo> infosNoused=new ArrayList<>();
        infoList.forEach(unit->{
            if(unit.getIsUsed()== UsedType.USED.getCode()){
                infos.add(unit);
            }else{
                infosNoused.add(unit);
            }
        });*/
        if(infoList.size() > 0){
            unitRepository.deleteUnitInfoMore(infoList);
        }
       /* if(infos.size() > 0){
            StringBuilder stringBuilder=new StringBuilder();
            infos.stream().forEach(c-> stringBuilder.append(",").append(c.getNumber()));
            return new ApiResult(3008, "", "序号" + stringBuilder.deleteCharAt(0).toString() + "已被使用无法删除", null);
        }*/
        return ApiResult.succ();
    }

    @Override
    public ApiResult<List<ConsumablesUnitInfo>> queryUnitInfoAll() {
        List<ConsumablesUnitInfo> infos = unitRepository.queryUnitInfo(null);
        return ApiResult.succ(infos);
    }

    @Override
    public ConsumablesUnitInfo getUnitDetailsInfoByInfo(ConsumablesUnitInfo unitInfo) {

        return unitRepository.getUnitDetailsInfoByInfo(unitInfo);
    }

    @Override
    public ApiResult updateAmount(String id, String type) {
        ConsumablesUnitInfo unitDetailsInfo = unitRepository.getUnitDetailsInfo(id);
            unitDetailsInfo.getAmount();
            ConsumablesUnitInfo consumablesUnitInfo = new ConsumablesUnitInfo();
            consumablesUnitInfo.setId(unitDetailsInfo.getId());
            if(UsedAmont.ADD.getIdentifier().equals(type)){
                consumablesUnitInfo.setAmount(unitDetailsInfo.getAmount()+1);
            }else{
                if(unitDetailsInfo.getAmount()==0){
                    return ApiResult.succ();
                }
                consumablesUnitInfo.setAmount(unitDetailsInfo.getAmount()-1);
            }
            unitRepository.updateUnitInfo(consumablesUnitInfo);
        return ApiResult.succ();
    }
}
